package com.cema.manage.project.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String timeToStr(Long time, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        if (time.toString().length() < 13) {
            time = time * 1000L;
        }
        Date date = new Date(time);
        String value = dateFormat.format(date);
        return value;
    }

    //通过生日日期计算年龄
    public static Integer calculateAge(Object docBirthday) {
        if (docBirthday != null) {
            Calendar cal = Calendar.getInstance();

            if (cal.before((Date) docBirthday)) {
                throw new IllegalArgumentException(
                        "The birthDay is before Now.It's unbelievable!");
            }

            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH) + 1;
            int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

            cal.setTime((Date) docBirthday);
            int yearBirth = cal.get(Calendar.YEAR);
            int monthBirth = cal.get(Calendar.MONTH) + 1;
            int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

            int age = yearNow - yearBirth;

            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    // monthNow==monthBirth
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        age--;
                    }
                } else {
                    // monthNow>monthBirth
                    age--;
                }
            }
            return age;
        } else {
            return null;
        }
    }

    public static void getCurrentDayTime(EntityWrapper entityWrapper, String createTime, String column) {
        if (StrUtil.isNotEmpty(createTime)) {
            LocalDate today = LocalDate.parse(createTime);
            LocalDateTime startOfDay = LocalDateTime.of(today, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(today, LocalTime.MAX);
            // 转换为系统默认时区的时间
            Date start = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            Date end = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
            // 设置查询条件
            entityWrapper.between(column, start, end);
        }
    }

    public static void getCurrentDayTime(EntityWrapper entityWrapper, String column) {
        LocalDate today = LocalDate.now();
        CreateDay createDay = new CreateDay(today).invoke();
        Date start = createDay.getStart();
        Date end = createDay.getEnd();
        // 设置查询条件
        entityWrapper.between(column, start, end);
    }

    public static List<TimeRange> get24HourTimeRanges(Date date) {
        List<TimeRange> timeRanges = new ArrayList<>();

        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), java.time.ZoneId.systemDefault());

        for (int i = 0; i < 24; i++) {
            // 使用模运算确保小时数在0到23之间
            int hour = i % 24;
            LocalDateTime startTime = localDateTime.withHour(hour).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endTime = localDateTime.withHour(hour).withMinute(59).withSecond(59).withNano(999999999);
            timeRanges.add(new DateUtils.TimeRange(startTime, endTime));
        }
        return timeRanges;
    }

    public static class TimeRange {
        private LocalDateTime startTime;
        private LocalDateTime endTime;

        public TimeRange(LocalDateTime startTime, LocalDateTime endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }

        public LocalDateTime getStartTime() {
            return startTime;
        }

        public LocalDateTime getEndTime() {
            return endTime;
        }
    }

    private static class CreateDay {
        private LocalDate today;
        private Date start;
        private Date end;

        public CreateDay(LocalDate today) {
            this.today = today;
        }

        public Date getStart() {
            return start;
        }

        public Date getEnd() {
            return end;
        }

        public CreateDay invoke() {
            LocalDateTime startOfDay = LocalDateTime.of(today, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(today, LocalTime.MAX);
            // 转换为系统默认时区的时间
            start = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            end = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
            return this;
        }
    }
}
